﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using JustinAngel.Neurons.Framework;
using JustinAngel.UnitedNationsNews.ItemViewMoels;
using JustinAngel.UnitedNationsNews.ServerModel;
using System.Linq;

namespace JustinAngel.UnitedNationsNews.Framework
{
    public class RssItemsToViewModelsTransformer : IRssItemsToViewModelsTransformer
    {
        public RootModel Transform(RssRootItem root)
        {
            var ReturnValue = new RootModel();

            ReturnValue.Photos = root.Value.Items.Where(r => r.Section == "photos").Select(ConvertToPhoto).ToList();
            ReturnValue.Videos = root.Value.Items.Where(r => r.Section == "videos").Select(ConvertToVideo).Where(item => item != null).ToList();
            ReturnValue.Radio = root.Value.Items.Where(r => r.Section == "radio").Select(ConvertToRadio).ToList();
            ReturnValue.Latest = root.Value.Items.Where(r => r.Section == "latest").Select(ConvertToArticle).ToList();
            ReturnValue.Articles = root.Value.Items.Where(r => r.Section == "article").Select(ConvertToArticle).ToList();

            return ReturnValue;
        }

        private PhotoItemViewModel ConvertToPhoto(RssItem rssItem)
        {
            return new PhotoItemViewModel()
            {
                Title = rssItem.Title,
                ImageUri = rssItem.Enclosure.Url,
                Description = rssItem.Description,
                Link = rssItem.Link
            };
        }

        private VideoItemViewModel ConvertToVideo(RssItem rssItem)
        {
            if (rssItem.MediaGroup == null || rssItem.MediaGroup.Thumbnails == null)
            {
                return null;
            }

            return new VideoItemViewModel()
            {
                Title = rssItem.Title,
                YouTubeUrl = rssItem.Link,
                ImageUri = rssItem.MediaGroup.Thumbnails.First(m => m.Width == 120).Url
            };
        }

        private RadioItemViewModel ConvertToRadio(RssItem rssItem)
        {
            return new RadioItemViewModel()
                       {
                           Title = rssItem.Title,
                           Mp3Uri = rssItem.Enclosure.Url
                       };
        }

        private ArticleItemViewModel ConvertToArticle(RssItem rssItem)
        {
            return new ArticleItemViewModel()
                       {
                           HtmlContent = rssItem.Html != null ? HttpUtility.HtmlDecode(rssItem.Html.Content) : string.Empty,
                           Title = rssItem.Title,
                           Link = rssItem.Link,
                           //PublicationDateString = rssItem.PublicationDate,
                           ImageUrl = rssItem.ImageUrl != null ? new Uri(rssItem.ImageUrl.Content, UriKind.Absolute) : null
                       };
        }
    }
}
